package com.mycms.module.index.controller;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.mycms.domain.Admin;
import com.mycms.domain.Login;
import com.mycms.module.index.service.AdminIndexService;
import com.mycms.util.StringUtil;
import com.mycms.validator.IdPasswordValidator;

@Controller
public class AdminIndexController {
	
	Logger logger = Logger.getLogger(this.getClass());
	
	@Autowired
	AdminIndexService adminIndexService;
	
	@Autowired
	private IdPasswordValidator idPasswordValidator;
	
	/**
	 * 관리자 메인
	 * 
	 * @param session
	 * @param msg
	 * @param login
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/admin/admin.do", method = RequestMethod.GET)
	public String adminIndex(HttpSession session, @RequestParam(required=false) String msg, @ModelAttribute Login login, Model model) {
				
		// 로그인 여부
		if( StringUtil.isEmpty(session.getAttribute("loginId")+"") ) {			
			return "index/login";
		}else {			
			return "index/main";
		}		
	}
	
	/**
	 * 관리자 로그인 처리
	 * 
	 * @param response
	 * @param session
	 * @param login
	 * @param bindingResult
	 * @return
	 */
	@RequestMapping(value="/admin/adminLogin.do", method = RequestMethod.POST) 
	public String adminLogin(HttpServletResponse response, HttpSession session, Login login, BindingResult bindingResult) {		
		
		// validation
		this.idPasswordValidator.validate(login, bindingResult);
		if (bindingResult.hasErrors()) {
			return "index/login";
		}
		
		// 로그인
		Admin admin = adminIndexService.login(login);
		if( admin != null ) {
			session.setAttribute("loginId", admin.getLoginId());
			return "redirect:admin.do";
		}else {			
			return "redirect:/alert.do?msg=loginFailur";						
		}		
	}	
	
	/**
	 * 관리자 로그아웃 처리
	 * 
	 * @param session
	 * @return
	 */
	@RequestMapping(value="/admin/adminLogout.do", method = RequestMethod.GET)
	public String adminLogout(HttpSession session) {
		session.invalidate();
		return "redirect:admin.do";
	}
	
	@RequestMapping(value="/admin/adminTopMenu.do", method = RequestMethod.GET)
	public String adminTopMenu(HttpSession session) {
		return "index/adminTopMenu";
	}
	
	@RequestMapping(value="/admin/adminLeftMenu.do", method = RequestMethod.GET)
	public String adminLeftMenu(HttpSession session) {
		return "index/adminLeftMenu";
	}
	
	@RequestMapping(value="/admin/adminContent.do", method = RequestMethod.GET)
	public String adminContent(HttpSession session) {
		return "index/adminContent";
	}
	
}

